<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card class="box-card">
        <!-- 使用 Tabs 组件完成标签页布局 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <!-- 使用 Layout 布局绘制头部区域 -->
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              class="department-header"
            >
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作
                        <i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="showDialogFn()"
                        >添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

            <!-- 树形结构 -->
            <el-tree
              :default-expand-all="true"
              :data="treeData"
              :props="defaultProps"
            >
              <!-- 插槽 -->
              <template v-slot="{ data }">
                <el-row
                  type="flex"
                  justify="space-between"
                  style="width: 100%"
                  align="middle"
                >
                  <!-- 组织架构 -->
                  <el-col :span="20">
                    <span class="companyItem">{{ data.name }}</span>
                  </el-col>
                  <!-- 负责人 & 操作 -->
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>负责人</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作
                            <i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                              @click.native="showDialogFn(data.id)"
                            >添加子部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>

        <!-- 编辑/新增 组织架构 弹框 -->
        <el-dialog
          width="50%"
          title="编辑/新增组织架构"
          :close-on-press-escape="false"
          :close-on-click-modal="false"
          :visible.sync="showDialog"
          :before-close="handleClose"
        >
          <deptdiglog
            :parent-id="parentId"
            @newDepartmentEvent="newDepartmentFn"
            @closeDialogEvent="closeDialogFn"
          />
          <!-- form表单 -->
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartmentsListAPI } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index.js'
import deptdiglog from './deptDiglog.vue'
export default {
  name: 'Departments',
  components: {
    deptdiglog
  },
  data() {
    return {
      activeName: 'first', // 被激活的 Tab 标签页
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false, // 是否显示弹框
      parentId: '' // 添加子部门的 父级ID
    }
  },
  created() {
    this.getDepartmentsListFn()
  },
  methods: {
    async getDepartmentsListFn() {
      const res = await getDepartmentsListAPI()
      this.treeData = tranListToTreeData(res.data.depts)
    },
    showDialogFn(Id) {
      this.showDialog = true
      this.parentId = Id
      console.log(this.parentId)
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    newDepartmentFn(resData) {
      this.getDepartmentsListFn()
      this.$message.success(resData.message)
      this.showDialog = false
    },
    closeDialogFn() {
      this.showDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .companyItem {
    margin-left: 12px;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
</style>
